import {createRouter, createWebHistory} from 'vue-router'

import routes from "./routes";
import store from "../service/store";

// 路由器实例对象
const router = createRouter({
    history: createWebHistory(),
    routes
});
//
const loginPath = '/login', // 登录路径
    anonymousPaths = [loginPath, '/reg']; // 匿名可访问路径
// 全局导航守卫
router.beforeEach((to, from, next) => {
    const toPath = to.path; // 获取要展示得路径
    if (anonymousPaths.includes(toPath) || /^\/s\/*/i.test(toPath)) next() // 判断是否可匿名访问
    else {
        // 验证token
        if (!store.getters.userToken) next(loginPath) // 没有登录直接打开登录页面
        // if(!localStorage.getItem('token')) next(loginPath)
        else next();
    }
})

export default router;